using System.Drawing;
using System.Drawing.Imaging;

namespace ICP_MS;

internal class ColorDistribution
{
	public static ColorMap map = new ColorMap();

	public unsafe static Bitmap GetImage(double[] buf, int width, int height)
	{
		Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
		BitmapData bData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
		byte[] rgb = new byte[3];
		byte* ptr = (byte*)(void*)bData.Scan0;
		int offset = bData.Stride - width * 3;
		for (int i = 0; i < height; i++)
		{
			for (int j = 0; j < width; j++)
			{
				map.GetColorAtValue(buf[i * width + j], ref rgb[0], ref rgb[1], ref rgb[2]);
				for (int c = 0; c < 3; c++)
				{
					*(ptr++) = rgb[3 - c - 1];
				}
			}
			ptr += offset;
		}
		bmp.UnlockBits(bData);
		return bmp;
	}
}
